package com.tj.collection.db.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.DateUtil;
import com.tj.collection.db.dao.ClAllocationBatchDao;
import com.tj.collection.db.entity.ClAllocationBatchEntity;
import com.tj.collection.db.entity.business.AllocatedLanguagesVo;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2019-09-03
 * Time: 10:18
 */
@Repository
public class ClAllocationBatchDaoImpl extends AbstractBaseMapper<ClAllocationBatchEntity> implements ClAllocationBatchDao {

    @Override
    public List<ClAllocationBatchEntity> getAllActiveBatch() {
        return getSqlSession().selectList(this.getStatement("getAllActiveBatch"));
    }

    @Override
    public List<ClAllocationBatchEntity> getAllActiveBatchByStageCode(String stageCode) {
        Map<String, String> map = new HashMap<>();
        map.put("stageCode", stageCode);
        return getSqlSession().selectList(this.getStatement("getAllActiveBatchByStageCode"), map);
    }

    @Override
    public List<ClAllocationBatchEntity> getWaittingRefreshBatch() {
        return getSqlSession().selectList(this.getStatement("getWaittingRefreshBatch"));
    }

    @Override
    public List<ClAllocationBatchEntity> getActiveListByAdminId(Integer adminId) {
        Map<String, Integer> map = new HashMap<>();
        map.put("adminId", adminId);
        return getSqlSession().selectList(this.getStatement("getActiveListByAdminId"), map);
    }

    @Override
    public List<AllocatedLanguagesVo> getAllocatedStatsOfLanguageByBatchNos(List<String> batchNos) {
        Map<String, Object> map = new HashMap<>();
        map.put("batchNos", batchNos);
        return getSqlSession().selectList(this.getStatement("getAllocatedStatsOfLanguageByBatchNos"), map);
    }

    @Override
    public List<ClAllocationBatchEntity> getActiveListByAdminIdAndStageCode(Integer adminId, String stageCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("adminId", adminId);
        map.put("stageCode", stageCode);
        return getSqlSession().selectList(this.getStatement("getActiveListByAdminIdAndStageCode"), map);
    }

    /**
     * 把日期小于今天且分配成功的批次 状态改成待刷新
     *
     * @return
     */
    @Override
    public Integer updateBatchSuccessToWaitingRefresh() {
        Map<String, Object> map = new HashMap<>();
        return getSqlSession().update(this.getStatement("updateBatchSuccessToWaitingRefresh"), map);
    }

    @Override
    public Integer countBatchByDate(Date date) {
        date = DateUtil.getPatternDate(date, "yyyy-MM-dd");
        Map<String, Object> map = new HashMap<>();
        map.put("createDt", date);
        return getSqlSession().selectOne(this.getStatement("countBatchByDate"), map);
    }
}
